"use strict";
const BaseImpl = require("../basics/clz/baseimpl");
const Result = require("../basics/result");
const Constant = require("../basics/constant");
const MasterModel = require("../model/master");
const DicsModel = require("../model/dics");
const WuserModel = require("../model/wuser");
const JumpModel = require("../model/jump");

/**
 * 通用服务
 */
class HomeImpl extends BaseImpl {

  /**
   * 查询基础配置
   */
  async queryBaseActConf() {
    return await DicsModel.getInstant(this.cloud).selectStrOne(Constant.DICS.BASE_ACT_CONF);
  }

  /**
   * 查询店铺
   * @param {String} appId 
   */
  async queryShopId(appId) {
    let master = await MasterModel.getInstant(this.cloud).selectOneByAppId(appId);
    if (!master) {
      master = await MasterModel.getInstant(this.cloud).selectOneByDef();
    }
    if (!master) {
      return null;
    }
    let openint = await DicsModel.getInstant(this.cloud).selectIntOne(Constant.DICS.BASE_OPEN);
    let isopen = false;
    if (openint == Constant.YesOrNo.yes) {
      isopen = true;
    }
    return { shopId: master.shopId, sellerId: master.sellerId, appId: master.appId, shopUrl: master.shopUrl, memberurl: master.memberurl, sessionKey: master.session, isopen, couponId: master.couponId };
  }

  /**
   * 查询店铺
   * @param {String} sellerId 
   */
  async queryShopOne(sellerId) {
    if (!sellerId) {
      let master = await MasterModel.getInstant(this.cloud).selectOneByDef();
      if (!master) {
        return null;
      }
      return { shopId: master.shopId, sellerId: master.sellerId, appId: master.appId, shopUrl: master.shopUrl, memberurl: master.memberurl, sessionKey: master.session, couponId: master.couponId };
    }
    const appid = await DicsModel.getInstant(this.cloud).selectStrOne(sellerId);
    if (!appid) {
      return null;
    }
    return await this.queryShopId(appid);
    // if (sellerId) {
    //   master = await MasterModel.getInstant(this.cloud).selectOneBySellerId(sellerId);
    // }
    // if (!master) {
    //   master = await MasterModel.getInstant(this.cloud).selectOneByDef();
    // }
    // if (!master) {
    //   return null;
    // }
    // return { shopId: master.shopId, sellerId: master.sellerId, appId: master.appId, memberurl: master.memberurl };
  }

  /**
   * 查询session
   * @param {String} appId 
   */
  async querySessionId(appId) {
    let master = await MasterModel.getInstant(this.cloud).selectOneByAppId(appId);
    if (!master) {
      master = await MasterModel.getInstant(this.cloud).selectOneByDef();
    }
    if (!master) {
      return null;
    }
    return { session: master.session };
  }

  /**
   * 查询白名单
   * @param {String} openId 
   */
  async queryIsWhite(openId) {
    return await WuserModel.getInstant(this.cloud).selectIsExistByUserNick(openId);
  }

  /**
   * 添加白名单
   * @param {String} openId 
   */
  async addWhite(openId) {
    const wuserModel = WuserModel.getInstant(this.cloud);
    const isexist = await wuserModel.selectIsExistByUserNick(openId);
    if (isexist < 1) {
      await wuserModel.insertOne({ userNick: openId });
    }
    return true;
  }

  /**
   * 查询配置
   */
  async findRebase() {
    const rebase = await DicsModel.getInstant(this.cloud).selectStrOne(Constant.DICS.REBASE_CTXT);
    if (!rebase) {
      return null;
    }
    return JSON.parse(rebase);
  }

  /**
   * 查询跳转
   * @param {*} code 
   */
  async findJump(code) {
    return await JumpModel.getInstant(this.cloud).selectOne(code);
  }
}

module.exports = HomeImpl;